using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkButton = Anthem.LinkButton;

namespace UsrControls
{
    public class LinkButtonTemplate : ITemplate
    {
        string _linkButtonTextField = String.Empty;
        string _linkButtonValueField = String.Empty;
        string _linkID = String.Empty;
        string _cssClass = String.Empty;
        string _textDuringCallBack = String.Empty;

        public string TextDuringCallBack
        {
            set
            {
                _textDuringCallBack = value;
            }
        }

        public event CommandEventHandler LinkButtonClick;


        public LinkButtonTemplate(string linkButtonTextField, string linkButtonValueField, string linkID, string cssClass)
        {
            _linkButtonTextField = linkButtonTextField;
            _linkButtonValueField = linkButtonValueField;
            _cssClass = cssClass;
            _linkID = linkID;
        }

        public LinkButtonTemplate(string linkButtonTextField, string linkButtonValueField, string linkID)
        {
            _linkButtonTextField = linkButtonTextField;
            _linkButtonValueField = linkButtonValueField;
            _linkID = linkID;
        }


        public void InstantiateIn(Control container)
        {
            Anthem.LinkButton lnkButton = new LinkButton();
            lnkButton.ID = _linkID;

            lnkButton.Command += new CommandEventHandler(LinkButton_Command);
            if (_cssClass != String.Empty)
            {
                lnkButton.Attributes.Add("class", _cssClass);
            }
            if (_linkButtonTextField != null)
            {
                lnkButton.DataBinding += new EventHandler(this.OnDataBinding);
            }
            container.Controls.Add(lnkButton);
        }
        private void LinkButton_Command(object sender, CommandEventArgs e)
        {
            // Raise event to the container 

            LinkButtonClick(sender, e);
        }


        public void OnDataBinding(object sender, EventArgs e)
        {
            Anthem.LinkButton lnkButton = (Anthem.LinkButton)sender;
            lnkButton.CausesValidation = false;
            DataGridItem container = (DataGridItem)lnkButton.NamingContainer;

            try
            {
                lnkButton.Text = ((DataRowView)container.DataItem)[_linkButtonTextField].ToString();
                if (_textDuringCallBack != String.Empty)
                {
                    lnkButton.TextDuringCallBack = _textDuringCallBack;
                }
                lnkButton.CommandArgument += DataBinder.Eval(container.DataItem, _linkButtonValueField);
                lnkButton.CommandName += DataBinder.Eval(container.DataItem, _linkButtonTextField);
            }
            catch
            {
                lnkButton.Text = _linkButtonTextField;
                if (_textDuringCallBack != String.Empty)
                {
                    lnkButton.TextDuringCallBack = _textDuringCallBack;
                }
                lnkButton.CommandArgument += _linkButtonValueField;
                lnkButton.CommandName += _linkButtonTextField;
            }
        }

    }
}
